


# to manipulate data
library(tidyverse)

# to estimate survival models
library(survival)

library(brglm2)
library(lmtest)
library(sandwich)

# to generate results tables
library(stargazer)

# load data
load("full_data_conditionality_covariates.RData")


# generate dataframe with survival-type data


## SHORT-TERM
survival_short_df <- full_df %>%
  filter(previous_short_term == 0) %>%
  select(iso3c,year,short_term,
         imf_program,resource_conditionality,resource_conditionality_dict_tfidf,
         resource_conditionality_count_abs,resource_conditionality_count_rel,resource_conditionality_dummy,
         resource_rents,log_gdp_pc_constant,gdp_growth,field_discovery,
         oil_price,crisis,polity2,left_executive,election,any_war,ideal_point_usa,eiti_member,
         nationalization_oc,wb_extractive_project,working_age_pop,t) %>%
  mutate(resource_conditionality = resource_conditionality * 100) %>%
  group_by(iso3c) %>%
  arrange(year, .by_group = T) %>%
  mutate(start2 = 1:length(iso3c),
         start = start2 - 1,
         stop = start + 1) %>%
  ungroup() %>%
  filter(year>1979)


## LONG-TERM
survival_long_df <- full_df %>%
  filter(previous_long_term == 0) %>%
  select(iso3c,year,long_term,
         imf_program,resource_conditionality,resource_conditionality_dict_tfidf,
         resource_conditionality_count_abs,resource_conditionality_count_rel,resource_conditionality_dummy,
         resource_rents,log_gdp_pc_constant,gdp_growth,field_discovery,
         oil_price,crisis,polity2,left_executive,election,any_war,ideal_point_usa,eiti_member,
         nationalization_oc,wb_extractive_project,working_age_pop,t) %>%
  mutate(resource_conditionality = resource_conditionality * 100) %>%
  group_by(iso3c) %>%
  arrange(year, .by_group = T) %>%
  mutate(start2 = 1:length(iso3c),
         start = start2 - 1,
         stop = start + 1) %>%
  ungroup() %>%
  filter(year>1979)



# Cox PH
cox1 <- coxph(Surv(start, stop, short_term) ~ imf_program + 
                log_gdp_pc_constant + gdp_growth + resource_rents + 
                working_age_pop + field_discovery + oil_price + #crisis + 
                wb_extractive_project +
                polity2 + left_executive + 
                election + #nationalization_oc + 
                #any_war + 
                eiti_member
              ,
              data = survival_short_df)


cox2 <- coxph(Surv(start, stop, short_term) ~ imf_program + resource_conditionality +
                log_gdp_pc_constant + gdp_growth + resource_rents + 
                working_age_pop + field_discovery + oil_price + #crisis + 
                wb_extractive_project +
                polity2 + left_executive + 
                election + #nationalization_oc + 
                #any_war + 
                eiti_member
              ,
              data = survival_short_df)

cox3 <- coxph(Surv(start, stop, short_term) ~ imf_program + resource_conditionality_dict_tfidf +
                log_gdp_pc_constant + gdp_growth + resource_rents + 
                working_age_pop + field_discovery + oil_price + #crisis + 
                wb_extractive_project +
                polity2 + left_executive + 
                election + #nationalization_oc + 
                #any_war + 
                eiti_member
              ,
              data = survival_short_df)


cox4 <- coxph(Surv(start, stop, long_term) ~ imf_program + 
                log_gdp_pc_constant + gdp_growth + resource_rents + 
                working_age_pop + field_discovery + oil_price + #crisis + 
                wb_extractive_project +
                polity2 + left_executive + 
                election + #nationalization_oc + 
                #any_war + 
                eiti_member
              ,
              data = survival_long_df)

cox5 <- coxph(Surv(start, stop, long_term) ~ imf_program + resource_conditionality +
                log_gdp_pc_constant + gdp_growth + resource_rents + 
                working_age_pop + field_discovery + oil_price + #crisis + 
                wb_extractive_project +
                polity2 + left_executive + 
                election + #nationalization_oc + 
                #any_war + 
                eiti_member
              ,
              data = survival_long_df)

cox6 <- coxph(Surv(start, stop, long_term) ~ imf_program + resource_conditionality_dict_tfidf +
                log_gdp_pc_constant + gdp_growth + resource_rents + 
                working_age_pop + field_discovery + oil_price + #crisis + 
                wb_extractive_project +
                polity2 + left_executive + 
                election + #nationalization_oc + 
                #any_war + 
                eiti_member
              ,
              data = survival_long_df)


# TABLE F.1
stargazer(cox1,cox2,cox3,cox4,cox5,cox6, #type = "text",
          dep.var.labels = c("Time To Short-Term Policy (Models 1--3)","Time To Long-Term Policy (Models 4--6)"),
          covariate.labels = c("Program Participation = 1",
                               "Resource Conditionality (%)",
                               "Resource Conditionality (TF--IDF)",
                               "GDP per Capita (Log)",
                               "GDP Growth (%)",
                               "Resource Rents (% GDP)",
                               "Working Age Population (%)",
                               "Field Discovery = 1",
                               "Oil Price (USD)",
                               #"Crisis = 1",
                               "WB Extractive Project",
                               "Democracy (Polity2)",
                               "Left Executive = 1",
                               "Election Year = 1",
                               #"Oil Company Nationalization = 1",
                               #"War = 1",
                               "EITI Member = 1"),
          no.space = T)

